package com.zodiac.app.szse.d.d0602.d060201;

import com.zodiac.app.szse.Utils;
import com.zodiac.app.szse.parse.Pdf2HtmlConverter;
import com.zodiac.app.szse.parse.Pdf2TextConverter;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Component
public class D060201Converter {
    /**
     * 将PDF报告数据转换为HTML
     *
     * @param request
     * @return
     */
    public Request convertToHtml(Request request) {
        final D060201Model model = request.getModel();
        try {
            final File pdf = getPdfFile(request);
            final Pdf2HtmlConverter converter = Pdf2HtmlConverter.create(pdf).convert();
            model.setHtmlConvertLog(converter.getLog());
            model.setHtmlConvertStatus(converter.isHtmlGenerated() ? D060201Model.HtmlConvertStatus.SUCCESSFUL : D060201Model.HtmlConvertStatus.FAILED);
            return request;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            model.setHtmlConvertLog(e.getMessage());
            model.setHtmlConvertStatus(D060201Model.HtmlConvertStatus.FAILED);
            return request;
        }
    }

    /**
     * 将PDF转换为Text文本
     *
     * @param request
     * @return
     */
    public Request convertToText(Request request) {
        final File pdf = getPdfFile(request);
        final Pdf2TextConverter converter = Pdf2TextConverter.create(pdf);
        final Pdf2TextConverter.ConvertType[] types = Pdf2TextConverter.ConvertType.values();
        for (Pdf2TextConverter.ConvertType type : types) {
            try {
                final List<String> lines = converter.convert(type);
                final List<String> filtered = Utils.removeHeaderWithFooterLine(Arrays.asList(String.join(System.lineSeparator(), lines).split(System.lineSeparator())));
                final File file = new File(pdf.getParentFile(), type.getFilename());
                Files.write(file.toPath(), String.join(System.lineSeparator(), filtered).getBytes(StandardCharsets.UTF_8));
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                request.getModel().setTextConvertStatus(D060201Model.TextConvertStatus.FAILED);
                return request;
            }
        }
        request.getModel().setTextConvertStatus(D060201Model.TextConvertStatus.SUCCESSFUL);
        return request;
    }

    /**
     * 构建保存路径
     *
     * @param request
     * @return
     */
    private File getPdfFile(Request request) {
        D060201Model model = request.getModel();
        final String year = model.getReportYear();
        final String month = D060201Extractor.ReportCategory.valueOf(model.getReportCategory()).getMonth();
        final String stockCode = model.getStockCode();
        String[] reportPathItems = model.getReportPath().split("/");
        String reportFileName = reportPathItems[reportPathItems.length - 1];

        return Paths.get(request.getStorePath().getAbsolutePath(), year + month, stockCode, reportFileName).toFile();
    }

    @Builder
    @Data
    public static class Request {
        private File storePath;
        private D060201Model model;
        private String table;
    }

}
